package fr.upmc.i3s.fuglmeyeranalyser.tools.xls;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import fr.upmc.i3s.fuglmeyeranalyser.models.solutions.AbstractSolution;
import fr.upmc.i3s.fuglmeyeranalyser.models.solutions.SolutionList;
import fr.upmc.i3s.fuglmeyeranalyser.patients.PatientParameter;

public class XLSGenerator {
	
	public static void generateXLSFileFromSolutionsList(SolutionList solutions, LinkedList<String> params, String outFile) throws URISyntaxException {
		//Create new workbook
		HSSFWorkbook workbook = new HSSFWorkbook();
		
		//Create new sheet
		HSSFSheet sheet = workbook.createSheet("Feuil1");
		 
		//Fill map with datas for output file
		HashMap<Integer, Object[]> mapRow = new HashMap<Integer, Object[]>();
		
		//Fill row with parameter names
		Object[] parameters = new Object[params.size()+solutions.get(0).getSolutionParameterNames().size()];
		int i = 0;
		for(String s : params) {
			parameters[i] = s;
			i++;
		}
		for(String s : solutions.get(0).getSolutionParameterNames()) {
			parameters[i] = s;
			i++;
		}
		mapRow.put(0, parameters);
		
		//Fill rows with parameters values for each patient solution
		int p = 1;
		for(AbstractSolution s : solutions) {
			Object[] patientValues = new Object[params.size()+solutions.get(0).getSolutionParameterNames().size()];
			int j = 0;
			for(PatientParameter pp : s.getPatient().getParamsList()) {
				patientValues[j] = pp.getValue();
				j++;
			}
			for(double d : s.getSolutionParameterValues()) {
				patientValues[j] = d;
				j++;
			}
			mapRow.put(p, patientValues);
			p++;
		}
		
		//Build XLS file
		for (int rownum=0; rownum<mapRow.size(); rownum++) {
		    Row row = sheet.createRow(rownum);
		    Object [] objArr = mapRow.get(rownum);
		    
		    int cellnum = 0;
		    for (Object obj : objArr) {
		        Cell cell = row.createCell(cellnum++);
		        
		        if(obj instanceof Date) {
		        	cell.setCellValue((Date) obj);
		        	
		        } else if(obj instanceof Boolean) {
		            cell.setCellValue((Boolean) obj);
		            
		    	} else if(obj instanceof String) {
		            cell.setCellValue((String) obj);
		        
		    	} else if(obj instanceof Double) {
		            cell.setCellValue((Double) obj);
		    	}
		    }
		}
		
		//Auto re-size columns
		for(int c=0; c<sheet.getRow(0).getLastCellNum(); c++) {
			sheet.autoSizeColumn(c);
		}
		 
		try {
            // generate file
		    FileOutputStream out = new FileOutputStream(new File(outFile));
		    workbook.write(out);
		    out.close();
		    System.out.println("XLS result file successfully generated at : "+outFile);
		     
		} catch (FileNotFoundException e) {
		    System.err.println("FileNotFoundException : "+e);
		    
		} catch (IOException e) {
			System.err.println("IOException : "+e);
		}
	}

}
